import requestInstance from '@/utils/request'

/**
   * 获取角色列表* @returns {Promise} 角色列表
 */
export const getRoles = async () => {
  try {
    const response = await requestInstance.get('roles');
    return response.data;
  } catch (error) {
    console.error('获取角色列表失败:', error);
    throw error;
  }
};

/**
 * 创建角色
 * @param {Object} roleData - 角色数据
 * @returns {Promise} 创建结果
 */
export const createRole = async (roleData) => {
  try {
    const response = await requestInstance.post('roles', roleData);
    return response.data;
  } catch (error) {
    console.error('创建角色失败:', error);
    throw error;
  }
};

/**
 * 更新角色
 * @param {string|number} id - 角色ID
 * @param {Object} roleData - 角色数据
 * @returns {Promise} 更新结果
 */
export const updateRole = async (id, roleData) => {
  try {
    const response = await requestInstance.put(`/api/roles/${id}`, roleData);
    return response.data;
  } catch (error) {
    console.error('更新角色失败:', error);
    throw error;
  }
};

/**
 * 删除角色
 * @param {string|number} id - 角色ID
 * @returns {Promise} 删除结果
 */
export const deleteRole = async (id) => {
  try {
    const response = await requestInstance.delete(`/api/roles/${id}`);
    return response.data;
  } catch (error) {
    console.error('删除角色失败:', error);
    throw error;
  }
};

/**
 * 获取权限列表
 * @returns {Promise} 权限列表
 */
export const getPermissions = async () => {
  try {
    const response = await requestInstance.get('permissions');
    return response.data;
  } catch (error) {
    console.error('获取权限列表失败:', error);
    throw error;
  }
};

/**
 * 获取角色的权限列表
 * @param {string|number} roleId - 角色ID
 * @returns {Promise} 角色权限列表
 */
export const getRolePermissions = async (roleId) => {
  try {
    const response = await requestInstance.get(`/api/roles/${roleId}/permissions`);
    return response.data;
  } catch (error) {
    console.error('获取角色权限失败:', error);
    throw error;
  }
};

/**
 * 更新角色权限
 * @param {string|number} roleId - 角色ID
 * @param {Array} permissionIds - 权限ID数组
 * @returns {Promise} 更新结果
 */
export const updateRolePermissions = async (roleId, permissionIds) => {
  try {
    const response = await requestInstance.put(`/api/roles/${roleId}/permissions`, {
      permissionIds
    });
    return response.data;
  } catch (error) {
    console.error('更新角色权限失败:', error);
    throw error;
  }
};

/**
 * 检查用户是否有权限
 * @param {string} permission - 权限键名
 * @returns {Promise} 权限检查结果
 */
export const checkPermission = async (permission) => {
  try {
    const response = await requestInstance.get('auth/check-permission', {
      params: {
        permission
      }
    });
    return response.data;
  } catch (error) {
    console.error('权限检查失败:', error);
    throw error;
  }
};

/**
 * 检查用户是否有角色
 * @param {string} role - 角色键名
 * @returns {Promise} 角色检查结果
 */
export const checkRole = async (role) => {
  try {
    const response = await requestInstance.get('auth/check-role', {
      params: {
        role
      }
    });
    return response.data;
  } catch (error) {
    console.error('角色检查失败:', error);
    throw error;
  }
};

/**
 * 用户登录
 * @param {Object} credentials - 登录凭证
 * @param {string} credentials.email - 邮箱
 * @param {string} credentials.password - 密码
 * @returns {Promise} 登录结果
 */
export const login = async (credentials) => {
  try {
    const response = await requestInstance.post('auth/login', credentials);
    return response.data;
  } catch (error) {
    console.error('登录失败:', error);
    throw error;
  }
};

/**
 * 用户登出
 * @returns {Promise} 登出结果
 */
export const logout = async () => {
  try {
    const response = await requestInstance.post('auth/logout');
    return response.data;
  } catch (error) {
    console.error('登出失败:', error);
    throw error;
  }
};

/**
 * 检查用户是否是管理员
 * @returns {Promise} 是否是管理员
 */
export const isAdmin = async () => {
  try {
    const response = await requestInstance.get('auth/is-admin');
    return response.data;
  } catch (error) {
    console.error('管理员检查失败:', error);
    // 出错时默认不是管理员
    return false;
  }
};